package com.tom.config;

import com.tom.constant.ResultEnum;
import lombok.Data;
import com.tom.exception.CommonError;

import java.util.HashMap;
import java.util.Objects;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @version V1.0
 * @Package tom-mail
 * @Description:
 * @author: dengyi
 * @date: 2019/11/4
 * @Copyright: 2019 www.tomonline-inc.com Inc. All rights reserved.
 * 注意：本内容仅限于TOM集团内部传阅，禁止外泄以及用于其他的商业目
 */
@Data
public class CustomResponse extends HashMap<String, Object> {

    private static final long serialVersionUID = -5831518211435146234L;

    private final static String CODE = "Code";
    private final static String MESSAGE = "Message";
    private static CustomResponse customResponse;

    private CustomResponse(ResultEnum resultEnum) {
        put(CODE, resultEnum.getCode());
        put(MESSAGE, resultEnum.getMessage());
    }

    private CustomResponse(CommonError commonError) {
        put(CODE, commonError.getCode());
        put(MESSAGE, commonError.getMessage());
    }


    private CustomResponse() {
        this.clear();
    }

    public CustomResponse(Integer Code, String msg) {
        put(CODE, Code);
        put(MESSAGE, msg);
    }


    public static CustomResponse ok() {
        return new CustomResponse(ResultEnum.SUCCESS);
    }

    public static CustomResponse fail() {
        return new CustomResponse(CommonError.FAIL);
    }

    public static CustomResponse fail(Integer code,String msg) {
        return new CustomResponse(code,msg);
    }

    public static CustomResponse state(ResultEnum resultEnum) {
        return new CustomResponse(resultEnum);
    }

    public static CustomResponse create() {
            customResponse = new CustomResponse();
        return customResponse;
    }


    public CustomResponse total(Object total) {
        put("total", total);
        return this;
    }

    public CustomResponse totalPage(Object totalPage) {
        put("totalPage", totalPage);
        return this;
    }

    public CustomResponse data(Object data) {
        put("mailbox", data);
        return this;
    }

    public CustomResponse body(Object... objects) {
        if (Objects.isNull(objects) || objects.length == 0 || (objects.length & 1) == 1) {
            throw new RuntimeException("parameters size It must be a multiplier of 2");
        }
        for (int i = 0; i < objects.length; i += 2) {
            put((String) objects[i], objects[i + 1]);
        }
        return this;
    }


}
